//
//  HomeHeaderView.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/7/27.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HomeHeaderView.h"
#import "SDCycleScrollView.h"
#import "HW3DBannerView.h"
#import "HGHomeModel.h"
#import "HGGoodsModel.h"

static CGFloat margin = 15;

@interface HomeHeaderView ()<SDCycleScrollViewDelegate>
{
    SDCycleScrollView *_bannerView;//图片轮播
    NSArray *_screenImgs;
}
@end

@implementation HomeHeaderView

- (instancetype)init {
    if (self = [super init]) {
        [self initSubviews];
    }
    return self;
}

- (void)setImageArray:(NSArray *)imageArray {
    _imageArray = imageArray;
    NSMutableArray *imageURLGroup = [@[] mutableCopy];
    [imageArray enumerateObjectsUsingBlock:^(HGHomeModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [imageURLGroup addObject:obj.imageUrl];
    }];
    _bannerView.imageURLStringsGroup = imageURLGroup;
}

- (void)setImages:(NSArray *)images {
    _bannerView.imageURLStringsGroup = images;
}

#pragma mark - Init
-(void)initSubviews
{
    _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@""]];
    _bannerView.currentPageDotColor = [UIColor redColor];
    _bannerView.pageDotColor = [UIColor lightGrayColor];;
    
    [self addSubview:_bannerView];
    
    [_bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

@end


@implementation HomeSecondView {
    HW3DBannerView *_scrollView;
//    SDCycleScrollView *_titleView;//文字轮播
}

- (instancetype)init {
    if (self = [super init]) {
        [self initSubviews];
    }
    return self;
}

- (void)setHomeModels:(NSArray<HGHomeModel *> *)homeModels {
    _homeModels = homeModels;
    NSMutableArray *imgUrls = [@[] mutableCopy];
//    NSMutableArray *titles = [@[] mutableCopy];
    [homeModels enumerateObjectsUsingBlock:^(HGHomeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [imgUrls addObject:obj.thumb];
//        [titles addObject:obj.desc];
    }];
    
    _scrollView.data = imgUrls;
//    _titleView.titlesGroup = titles;
}

#pragma mark - Init
-(void)initSubviews
{
    _scrollView = [HW3DBannerView initWithFrame:CGRectMake(0, 0, kScreenWidth, 150) imageSpacing:10 imageWidth:kScreenWidth - 50];
    [self addSubview:_scrollView];
    
//    _titleView=[[SDCycleScrollView alloc]initWithFrame:CGRectMake(40 * kScale, 150 - 30, kScreenWidth - 80 * kScale, 30)];
//    [self addSubview:_titleView];
    
    [self layoutIfNeeded];
    WS(weakSelf)
    _scrollView.initAlpha = 0.5; // 设置两边卡片的透明度
    _scrollView.imageRadius = 10; // 设置卡片圆角
    _scrollView.imageHeightPoor = 10; // 设置中间卡片与两边卡片的高度差
    _scrollView.autoScroll = NO;
    _scrollView.placeHolderImage = [UIImage imageNamed:@"jiazai"]; // 设置占位图片
    _scrollView.showPageControl = NO;
    _scrollView.clickImageBlock = ^(NSInteger currentIndex) { // 点击中间图片的回调
        if (weakSelf.callBack) {
            weakSelf.callBack(weakSelf.homeModels[currentIndex].url);
        }
    };
    
//    _titleView.scrollDirection=UICollectionViewScrollDirectionVertical;
//    _titleView.backgroundColor=[UIColor clearColor];
//    _titleView.onlyDisplayText=YES;
//    _titleView.showPageControl=NO;
//    _titleView.titleLabelBackgroundColor=[UIColor clearColor];
//    _titleView.titleLabelTextFont=[UIFont systemFontOfSize:15*kScale];
//    _titleView.titleLabelTextColor=[UIColor whiteColor];
//    _titleView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
}

@end

@interface HomeGoodsView ()

@property (strong, nonatomic)UIImageView *goodsimage;

@property (strong, nonatomic)UILabel *goodsNameLabel;

@property (strong, nonatomic)UILabel *goodsMoenyLabel;

@end

@implementation HomeGoodsView

- (instancetype)init {
    if (self = [super init]) {
        [self initSubViews];
    }
    return self;
}

- (void)setModel:(HGGoodsModel *)model {
    _model = model;
    
    [_goodsimage sd_setImageWithURL:kPhotoURL(model.faceImg) placeholderImage:nil options:(SDWebImageProgressiveDownload)];
    _goodsNameLabel.text = model.name;
    _goodsMoenyLabel.text = [NSString stringWithFormat:@"￥ %@",model.rate];
}

- (void)initSubViews {
    
    _goodsimage = [[UIImageView alloc] init];
    _goodsimage.image = [UIImage imageNamed:@"jiazai"];
    [self addSubview:_goodsimage];
    
    [_goodsimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.offset(margin);
        make.size.mas_equalTo(CGSizeMake(130, 130));
    }];
    
    _leftImage = [[UIImageView alloc] init];
    [_goodsimage addSubview:_leftImage];
    
    [_leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(_goodsimage);
    }];
    
    _goodsNameLabel = [[UILabel alloc] init];
    _goodsNameLabel.font = [UIFont systemFontOfSize:12];
    _goodsNameLabel.textColor = kColorText;
    _goodsNameLabel.text = @"拉菲庄园至尊红酒";
    [self addSubview:_goodsNameLabel];
    
    [_goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsimage);
        make.top.equalTo(self.goodsimage.mas_bottom).offset(margin/3);
    }];
    
    _goodsMoenyLabel = [[UILabel alloc] init];
    _goodsMoenyLabel.font = [UIFont systemFontOfSize:12];
    _goodsMoenyLabel.textColor = [UIColor redColor];
    _goodsMoenyLabel.text = @"￥6888.00";
    [self addSubview:_goodsMoenyLabel];
    
    [_goodsMoenyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsNameLabel);
        make.top.equalTo(self.goodsNameLabel.mas_bottom).offset(margin/3);
    }];
    
}

@end

@implementation DistrictHead

- (instancetype)initWithTitle:(NSString *)title {
    if (self = [super init]) {
        
        UIButton *leftbutton = [[UIButton alloc] init];
        [leftbutton setImage:[UIImage imageNamed:title] forState:(UIControlStateNormal)];
        [leftbutton setTitle:title forState:(UIControlStateNormal)];
        leftbutton.titleLabel.font = [UIFont systemFontOfSize:16];
        [leftbutton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [leftbutton layoutButtonWithEdgeInsetsStyle:(LGButtonEdgeInsetsStyleLeft) imageTitleSpace:5];
        [self addSubview:leftbutton];
        
        [leftbutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.offset(margin);
        }];
        
        UIButton *rithButton = [[UIButton alloc] init];
        [rithButton setImage:[UIImage imageNamed:@"more"] forState:(UIControlStateNormal)];
        [self addSubview:rithButton];
        
        [rithButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftbutton);
            make.right.offset(-margin);
        }];
    }
    return self;
}

@end

